﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using _1041448_DH_Utilities;

namespace _1041448_Week01
{
    public partial class Form1 : Form
    {
        private string fName = string.Empty;
        private string outName = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }

        private void InputProccessButton_Click(object sender, EventArgs e)
        {
            if (fName.Equals("")) { MessagesInputProcessLabel.Text = "Please choose file"; return; }

            int maxBit = 0;
            string error = string.Empty;
            string resouce = string.Empty;
            bool isReadSuccess;

            // Find any word, that contain spaces
            string source = _1041448_DH_Utilities.Files.ReadFile(fName, out isReadSuccess);

            if (isReadSuccess) { MessagesInputProcessLabel.Text = "Read file failed"; return; }

            string watermark = this.InputWatermarkTextbox.Text;

            if (!_1041448_Watermarks.Spaces.PreprocessSource(ref source, watermark, ref maxBit, out error))
            {
                MessagesInputProcessLabel.Text = error;
                return;
            }

            MaxBitLabel.Text = maxBit.ToString();

            watermark = _1041448_Watermarks.Spaces.PreprocessWatermark(watermark);

            _1041448_Watermarks.Spaces.InputWatermark(ref source, watermark);

            if (!_1041448_DH_Utilities.Files.SaveFile(fName, source)) { MessagesInputProcessLabel.Text = "Save file failed !"; return; }

            MessagesInputProcessLabel.Text = "Save file successfully ";
        }

        private void BrowseInputFileButton_Click(object sender, EventArgs e)
        {

            DialogResult rs = openFileDialog1.ShowDialog();
            if (rs == DialogResult.OK)
            {
                fName = openFileDialog1.FileName;
                MessagesInputProcessLabel.Text = "Process input watermark ...";
            }
            else if (rs == DialogResult.Cancel)
            {
                MessagesInputProcessLabel.Text = "Waiting";
            }

        }

        private void BrowseOutputFileButton_Click(object sender, EventArgs e)
        {
            DialogResult rs = openFileDialog2.ShowDialog();
            if (rs == DialogResult.OK)
            {
                outName = openFileDialog2.FileName;
                MessagesInputProcessLabel.Text = "Process get watermark ...";
            }
            else if (rs == DialogResult.Cancel)
            {
                MessagesOutputProcessLabel.Text = "Waiting";
            }
        }

        private void OutputProcessButton_Click(object sender, EventArgs e)
        {
            if (outName.Equals("")) { MessagesOutputProcessLabel.Text = "Please choose file"; return; }
            bool isReadSuccess = false;
            bool isGetWatermark = false;
            string watermark = string.Empty;
            string messages = string.Empty;

            // Read file
            // Find any word, that contain spaces
            string source = _1041448_DH_Utilities.Files.ReadFile(outName, out isReadSuccess);

            // Preprocess source
            source = _1041448_Watermarks.Spaces.PreprocessOutputSource(source);

            watermark = _1041448_Watermarks.Spaces.GetWaterwark(source, out isGetWatermark, out messages);

            OutputWatermarkTextBox.Text = watermark;
        }


    }
}
